﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ODataRegression.Framework.Core.MetaDataOM
{
   public  class ODataEntitySet
    {
        private string url;
        private string collectionName;
        private string entityName;
        //XElement entityMetadata;
        private ODataEntity entity;
       public string serviceName;
        public string URL { get { return this.url; } }
        public string CollectionName { get { return this.collectionName; } }
        //public string EntityName { get { return this.entityName; } }
        public ODataEntity Entity { get { return this.entity; } }
       // XElement EntityMetadata { get { return this.entityMetadata; } }

        public ODataEntitySet(string ServiceName,string url,string collectionName,XElement serviceMetadata )
        {
            this.serviceName = ServiceName;
            this.url = url;
            this.collectionName = collectionName;
           // this.entityMetadata = serviceMetadata;
            XElement schemaEntitySets= serviceMetadata.Elements().First().Elements().First();

          

           XElement schemaContainers = serviceMetadata.Elements().First().Elements().ToList()[1];
           schemaContainers=schemaContainers.Elements().ToList()[0];
           //XElement entityContainerMapping;

           bool isFoundAttrib = false;
           
            //Find EntitySet Vs Entity Mapping 
           foreach (XElement item in schemaContainers.Elements())
           {
               foreach (XAttribute attrib in item.Attributes())
               {
                   if (item.Name.LocalName=="EntitySet" && attrib.Value==collectionName)
                   {
                       this.entityName = item.Attributes().ToList()[1].Value;
                       this.entityName = this.entityName.Split('.')[this.entityName.Split('.').Length - 1];
                       isFoundAttrib = true;
                       break;
                   }
               }

               if (isFoundAttrib)
               {
                   break;
               }
           }

            // Now find Entity metadat definition 
           XElement entityMetadata = null;
            isFoundAttrib=false;
           foreach (XElement ent in schemaEntitySets.Elements())
           {

               foreach (XAttribute entAtrib in ent.Attributes())
               {
                   if (entAtrib.Value==this.entityName)
                   {
                       isFoundAttrib=true;
                       entityMetadata=ent;
                       break;

                   }
               }

               if (isFoundAttrib)
               {
                   break;
               }
           }

            ////Create entity object here 
           entity = new ODataEntity(this.entityName,this.CollectionName, entityMetadata);
        }
        


       
    }
}
